var express = require('express');
var fs = require('fs');
var mysql = require('mysql');
var app = express();

var pool = mysql.createPool({
	host: 'localhost',
	port: 3306,
	database: 'test',
	user : 'root',
	password: '123456'
});

app.get('/index.html', function(req, res){
	res.sendfile(__dirname + '/index3.html');
});

app.put('/index.html', function(req, res){
	req.on('data', function(data){
		console.log(data)
		// JSON.parse方法--将字符串转为一个对象
		var obj = JSON.parse(data.toString());

		pool.getConnection(function(err, connection){
			if(err)
				res.send('与MYSQL数据库建立连接失败');
			else{
				var str;
				connection.query('insert into mytable set ?',
				{name:obj.name, sex:obj.sex},
				function(err, result){
					if(err)
						str = '在服务器端MYSQL数据库中插入数据失败。';
					else
						str = '在服务器端MYSQL数据库中插入数据成功。';
					connection.release();
					res.send(str);
				});
			}
		})
	});
});


app.listen(1337, 'localhost');